import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import * as bodyParser from 'body-parser';
import * as compression from 'compression';
import { Logger } from '@nestjs/common';
import { CommonConfig } from './@nt';
import { HttpExceptionFilter } from './@nt/exception/http.exception.filter';
import { BussinessValidatePipe } from './@nt/validate/validate.pipe';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    // httpsOptions: sslOptions,
    cors: true,
  });

  app.use(bodyParser.raw({ limit: '256mb', type: 'application/json' }));
  app.use(bodyParser.json({ limit: '256mb' }));
  app.use(bodyParser.text({ limit: '1024mb' }));
  app.use(bodyParser.urlencoded({ limit: '256mb', extended: true }));
  app.use(
    compression({
      filter: function shouldCompress(req, res) {
        if (req.headers['x-no-compression']) {
          return false;
        }
        return compression.filter(req, res);
      },
    }),
  );
  app.useGlobalFilters(new HttpExceptionFilter());
  app.useGlobalPipes(new BussinessValidatePipe());
  app.enableShutdownHooks();
  await app.listen(CommonConfig.ENV.get('APPLICATION_PORT'));
  Logger.log(
    `listen port: ${CommonConfig.ENV.get('APPLICATION_PORT')}, env: ${process.env.NODE_ENV}`,
  );
}
function uncaughtExceptionHandler(err) {
  if (err && err.code === 'ECONNREFUSED') {
  }
  Logger.error(err.message);
}
process.on('uncaughtException', uncaughtExceptionHandler);
bootstrap();
